<?php
/**
 * 定时检测订单预警时间
 * Created by PhpStorm.
 * User: poptser
 * Email: poptser@foxmail.com
 * Date: 2016/8/25
 * Time: 16:52
 */

namespace YMApi\Controller;
use Think\Controller;
use Think\Model;

class SystemalarmcheckController extends Controller
{
    public $pagesize=1000;
    public function check(){
        $this->system_service_source_no();//检查库存系统工作平台数据
    }
    
    
    /*
     * 定时清空冻结预警日志表
     * */
    public function del_system_service_log(){
        $sql='delete from sdx_system_service_log';
        M()->execute($sql);
        
    }
     /*
     * 插入系统工作日志
     * */
    private function system_service_log($wid,$source_no,$type){
        $data_arr=array(
            'wid'=>$wid,
            'source_no'=>$source_no,
            'log_type'=>$type,
            'status'=>0
        );
        M('system_service_log')->add($data_arr);
    }
    
   /*
    * 检查系统工作日志是否已经存在
    */
    private function check_repeat_system_service_log($wid,$source_no,$type){
        $id=M('system_service_log')->where(array('wid'=>$wid,'source_no'=>$source_no,'log_type'=>$type))->getField('id');
        if($id>0){
            return false;
        }else{
            return true;
        }
    }
    
    /*
     * 冻存预警
     * */
     private function system_service_source_no(){
        ini_set('max_execution_time', '0');
        set_time_limit(0);
        //ini_set('memory_limit','4096M');
        $page=S('system_service_source_no_page');
        if(empty($page)||!is_numeric($page)){
            S('system_service_source_no_page',1,86400);
            $page=1;
        }
        $begin_num = ($page-1)*$this->pagesize;
        $goods_freeze_record_mod=M('goods_freeze_record');
        $system_service_log_mod=M('system_service_log');
        $system_service_log_mod->where(array('status'=>1,'log_type'=>1))->delete();
        $sql_count="SELECT COUNT(DISTINCT source_no) as s_count FROM sdx_goods_freeze_record WHERE wid = 8 AND record_type <> 13";
        /*$sql_count='SELECT COUNT(source_no) as s_count FROM('.
            'SELECT wid,source_no FROM sdx_goods_freeze_record WHERE wid=8 and record_type<>13 GROUP BY source_no) as tt';*/
        $s_count=M()->query($sql_count);
        $count=$s_count[0]['s_count'];
        $all_page = ceil($count/$this->pagesize);
        $gfrlist=$goods_freeze_record_mod->field('wid,source_no')->where(array('wid'=>8,'record_type'=>array('neq',13)))->group('source_no')->limit($begin_num.",".$this->pagesize)->select();
        if($gfrlist){
            $order_info_mod=M('order_info');
            $source_no_group=array();//来源单号分组
            foreach ($gfrlist as $key=>$val){
                if(!empty($val['source_no'])){
                    $source_no_2=substr($val['source_no'],0,2);
                    if(is_numeric($source_no_2)){
                        $oires=$order_info_mod->field('order_id,order_sn,batch_no,wid')->where(array('wid'=>$val['wid'],'order_sn'=>$val['source_no']))->select();
                        if($oires){
                            foreach ($oires as $k=>$v){
                                $v['source_no']=$v['order_sn'];
                                if($v['batch_no']==0){
                                    $source_no_group[$v['order_sn']][]=$v;
                                }else{
                                    $source_no_group[$v['batch_no']][]=$v;
                                }
                            }
                        }else{
                            $source_no_group[$val['source_no']][]=$val;
                        }
                    }else{
                        $source_no_group[$val['source_no']][]=$val;
                    }
              }
              unset($gfrlist[$key]);
            }
            $plus_status=array(2,4,7);
            $subtraction_status=array(1,3,5,6,8,9,10,11,12,15,14,16);
            foreach ($source_no_group as $key=>$val){
                if(count($val)>1){
                    $wid=$val[0]['wid'];
                    $source_no_str=array();
                    $plus=0;
                    $subtraction=0;
                    $plus_freeze_num=0;
                    $subtraction_freeze_num=0;
                    foreach ($val as $k=>$v){
                        $source_no_str[]=$v['source_no'];
                    }
                    $source_no=implode(',',$source_no_str);
                    $gfr_s=$goods_freeze_record_mod->field('wid,source_no,freeze_num,record_type')->where(array('source_no'=>array('in',$source_no),'wid'=>$wid))->select();
                    if($gfr_s){
                        foreach ($gfr_s as $kk=>$vv){
                            if($vv['wid']!=0){
                                if(in_array($vv['record_type'], $plus_status)){
                                    $plus++;
                                    $plus_freeze_num+=$vv['freeze_num'];
                                    unset($gfr_s[$kk]);
                                }
                                if(in_array($vv['record_type'], $subtraction_status)){
                                    $subtraction++;
                                    $subtraction_freeze_num+=$vv['freeze_num'];
                                    unset($gfr_s[$kk]);
                                }
                            }
                        }
                    }
                    dump($source_no_str);
                    echo $plus.'=>'.$subtraction.'<br />';
                    echo 'plus_freeze_num=>'.$plus_freeze_num.'=subtraction_freeze_num=>'.$subtraction_freeze_num.'<br />';
                    if($plus!==$subtraction){
                        if($plus_freeze_num!==$subtraction_freeze_num){
                            foreach ($source_no_str as $kkk=>$vvv){
                                if($this->check_repeat_system_service_log($wid,$vvv,1)){
                                    $this->system_service_log($wid,$vvv,1);
                                }else{
                                    continue;
                                }
                            }
                        }
                        unset($source_no_group[$key]);
                        continue;
                    }else{
                        foreach ($source_no_str as $kkk=>$vvv){
                            if($this->check_repeat_system_service_log($wid,$vvv,1)){
                                $system_service_log_mod->where(array('wid'=>$wid,'source_no'=>$vvv,'log_type'=>1))->save(array('status'=>1));
                            }else{
                                continue;
                            }
                        }
                        unset($source_no_group[$key]);
                        continue;
                    }
                }else{
                    $wid=$val[0]['wid'];
                    $source_no=$val[0]['source_no'];
                    $plus=0;
                    $subtraction=0;
                    $plus_freeze_num=0;
                    $subtraction_freeze_num=0;
                    $gfr_s=$goods_freeze_record_mod->field('wid,source_no,freeze_num,record_type')->where(array('source_no'=>$source_no,'wid'=>$wid))->select();
                    if($gfr_s){
                        foreach ($gfr_s as $kk=>$vv){
                            if($vv['wid']!=0){
                                if(in_array($vv['record_type'], $plus_status)){
                                    $plus++;
                                    $plus_freeze_num+=$vv['freeze_num'];
                                    unset($gfr_s[$kk]);
                                }
                                if(in_array($vv['record_type'], $subtraction_status)){
                                    $subtraction++;
                                    $subtraction_freeze_num+=$vv['freeze_num'];
                                    unset($gfr_s[$kk]);
                                }
                            }
                        }
                    }
                    echo $source_no.'=>'.$plus.'=>'.$subtraction.'<br />';
                    echo 'plus_freeze_num=>'.$plus_freeze_num.'=subtraction_freeze_num=>'.$subtraction_freeze_num.'<br />';
                    if($plus!==$subtraction){
                        if($plus_freeze_num!==$subtraction_freeze_num){   
                               if($this->check_repeat_system_service_log($wid,$source_no,1)){
                                    $this->system_service_log($wid,$source_no,1);
                                }else{
                                    continue;
                                }
                        }      
                        unset($source_no_group[$key]);
                         continue;
                        
                    }else{
                        if($this->check_repeat_system_service_log($wid,$source_no,1)){
                                $system_service_log_mod->where(array('wid'=>$wid,'source_no'=>$source_no,'log_type'=>1))->delete();
                        }else{
                                continue;
                        }
                        unset($source_no_group[$key]);
                        continue;
                    }
                }
                
            }
        $next_page = $page+1;
        S('system_service_source_no_page',$next_page,86400);
        if($next_page>$all_page){
            S('system_service_source_no_page',1,86400);
        }
    }
  }
    /*
     * 冻存预警
     * */
    private function system_service_source_no_bak(){
        ini_set('max_execution_time', '0');
        set_time_limit(0);
        //ini_set('memory_limit','4096M');
        $page=S('system_service_source_no_page');
        if(empty($page)||!is_numeric($page)){
            S('system_service_source_no_page',1,86400);
            $page=1;
        }
        $begin_num = ($page-1)*$this->pagesize;
        $goods_freeze_record_mod=M('goods_freeze_record');
        $goods_freeze_record_mod->where(array('status'=>1,'log_type'=>1))->delete();
        $sql_count="SELECT COUNT(DISTINCT source_no) as s_count FROM sdx_goods_freeze_record WHERE wid = 8 AND record_type <> 13";
        /*$sql_count='SELECT COUNT(source_no) as s_count FROM('.
                   'SELECT wid,source_no FROM sdx_goods_freeze_record WHERE wid=8 and record_type<>13 GROUP BY source_no) as tt';*/
        $s_count=M()->query($sql_count);
        $count=$s_count[0]['s_count'];
        $all_page = ceil($count/$this->pagesize);
        $gfrlist=$goods_freeze_record_mod->field('wid,source_no')->where(array('wid'=>8,'record_type'=>array('neq',13)))->group('source_no')->limit($begin_num.",".$this->pagesize)->select();
        if($gfrlist){
            $plus_status=array(2,4,7);
            $subtraction_status=array(1,3,5,6,8,9,10,11,12,15,14,16);
            foreach ($gfrlist as $key=>$val){
                if(!empty($val['source_no'])){
                    $gfr_s=$goods_freeze_record_mod->field('wid,source_no,record_type')->where(array('source_no'=>$val['source_no'],'wid'=>$val['wid']))->select();
                    if($gfr_s){
                            $plus=0;
                            $subtraction=0;
                            foreach ($gfr_s as $k=>$v){
                                if($v['wid']!=0){
                                    if(in_array($v['record_type'], $plus_status)){
                                            $plus++;
                                            unset($gfr_s[$k]);
                                    }
                                    if(in_array($v['record_type'], $subtraction_status)){
                                        $subtraction++;
                                        unset($gfr_s[$k]);
                                    }
                               }
                        }
                    }
                    echo $val['source_no'].'=>'.$plus.'=>'.$subtraction.'<br />';
                    if($plus!==$subtraction){
                        if($this->check_repeat_system_service_log($val['wid'],$val['source_no'],1)){
                            $this->system_service_log($val['wid'],$val['source_no'],1);
                        }else{
                            continue;
                        }
                    }else{
                        if($this->check_repeat_system_service_log($val['wid'],$val['source_no'],1)){
                            $goods_freeze_record_mod->where(array('wid'=>$val['wid'],'source_no'=>$val['source_no'],'log_type'=>$val['log_type']))->delete();
                        }else{
                            continue;
                        }
                    }
                }
                unset($gfrlist[$key]);
            }
        }
        $next_page = $page+1;
        S('system_service_source_no_page',$next_page,86400);
        if($next_page>$all_page){
            S('system_service_source_no_page',1,86400);
        }
    }
}